package com.xiaozhi.libraryfunction.service.impI;


import com.xiaozhi.common.constant.UserConstants;
import com.xiaozhi.common.core.text.Convert;
import com.xiaozhi.common.utils.StringUtils;
import com.xiaozhi.libraryfunction.domain.LibraryBook;
import com.xiaozhi.libraryfunction.domain.LibraryBook;
import com.xiaozhi.libraryfunction.mapper.LibraryBookMapper;
import com.xiaozhi.libraryfunction.mapper.LibraryBookMapper;
import com.xiaozhi.libraryfunction.service.LibraryBookService;
import com.xiaozhi.libraryfunction.service.LibraryBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 图书书籍信息 服务层
 * 
 * @author ruoyi
 */
@Service
public class LibraryBookServiceImpl implements LibraryBookService
{
    @Autowired
    private LibraryBookMapper libraryBookMapper;

    /**
     * 获取图书书籍列表
     *
     * @param book 书籍信息
     * @return
     */
    @Override
    public List<LibraryBook> selectLibraryBookList(LibraryBook book)
    {
        return libraryBookMapper.selectLibraryBookList(book);
    }
    /**
     * 通过书籍ID查询书籍
     *
     * @param bookId 书籍ID
     * @return 书籍对象信息
     */
    @Override
    public LibraryBook selectLibraryBookById(Long bookId)
    {
        return libraryBookMapper.selectLibraryBookById(bookId);
    }

    /**
     * 批量删除书籍信息
     *
     * @param ids 需要删除的数据ID
     * @throws Exception
     */
    @Override
    @Transactional
    public int deleteLibraryBookByIds(String ids)
    {
        Long[] typeIds = Convert.toLongArray(ids);
        return libraryBookMapper.deleteLibraryBookByIds(typeIds);
    }

    /**
     * 新增保存书籍信息
     *
     * @param book 书籍信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertLibraryBook(LibraryBook book)
    {
        // 新增书籍信息
        return libraryBookMapper.insertLibraryBook(book);
    }

    /**
     * 修改保存书籍信息
     *
     * @param book 书籍信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateLibraryBook(LibraryBook book)
    {
        // 修改书籍信息
        return libraryBookMapper.updateLibraryBook(book);
    }
    /**
     * 校验书籍名称是否唯一
     *
     * @param book 书籍信息
     * @return 结果
     */
    @Override
    public String checkLibraryBookNameUnique(LibraryBook book)
    {
        Long typeId = StringUtils.isNull(book.getBookId()) ? -1L : book.getBookId();
        LibraryBook info = libraryBookMapper.checkLibraryBookNameUnique(book.getName());
        if (StringUtils.isNotNull(info) && info.getTypeId().longValue() != typeId.longValue())
        {
            return UserConstants.LibraryBook_NAME_NOT_UNIQUE;
        }
        return UserConstants.LibraryBook_NAME_UNIQUE;
    }

    @Override
    public int updateLibraryBookAdd(Long bookId) {
        // 修改书籍信息数量+1
        return libraryBookMapper.updateLibraryBookAdd(bookId);
    }

    @Override
    public int updateLibraryBookCut(Long bookId) {
        // 修改书籍信息数量-1
        return libraryBookMapper.updateLibraryBookCut(bookId);
    }
}